﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Configuration;
using System.Windows.Forms;
using LineWatch_WinForm;

namespace DMSetup
{
    public partial class Form2 : Form
    {
        private SerialPortOperator sp;
        public Form2()
        {
            InitializeComponent();
            sp = new SerialPortOperator("COM1", 9600);
        }

        private void btnStopTime_Click(object sender, EventArgs e)
        {
            string dmData = txtStopTime.Text.Trim();
            int outData;
            if (!int.TryParse(dmData, out outData))
            {
                MessageBox.Show("数据不是数字类型");
                return;
            }
            SetUp("1086", outData * 60);
            MessageBox.Show("停止时间修改成功");
        }

        private void btnProdNum_Click(object sender, EventArgs e)
        {
            string dmData = txtProdNum.Text.Trim();
            int outData;
            if (!int.TryParse(dmData, out outData))
            {
                MessageBox.Show("数据不是数字类型");
                return;
            }
            SetUp("1140", outData);
            MessageBox.Show("实际生产台数修改成功");
        }

        private void SetUp(string dmAddress, int dmData)
        {
            string command = "@00WD" + dmAddress.PadLeft(4, '0') + dmData.ToString("X").PadLeft(4, '0');
            sp.WriteLine(command);
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            sp.ClosePort();
        }

        private void SetOffSetTime_Click(object sender, EventArgs e)
        {
            string text = textBox1.Text;
            int outV;
            if (!int.TryParse(text, out outV))
            {
                MessageBox.Show("请输入数字");
                return;
            }
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(ConfigurationManager.AppSettings["DataFilePath"].ToString().Trim());
            XmlElement node = (XmlElement)xmlDoc.SelectSingleNode("//add[@key='CTOffSetTime']");
            node.SetAttribute("value", outV.ToString());
            xmlDoc.Save(ConfigurationManager.AppSettings["DataFilePath"].ToString().Trim());
            MessageBox.Show("OffSetTime设定成功");
        }

        private void btnSetNowPlan_Click(object sender, EventArgs e)
        {
            string text = textBox2.Text;
            int outV;
            if (!int.TryParse(text, out outV))
            {
                MessageBox.Show("请输入数字");
                return;
            }
            SetUp("5005", outV);
            MessageBox.Show("现时生产台数修改成功");
        }
    }
}
